﻿#include "txt.h"
#include "ui_txt.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QFile>
#include <QFileInfo>
#include <QDesktopServices>
#include <QTextCodec>



Txt::Txt(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Txt)
{
    ui->setupUi(this);
    QTextCodec *codec = QTextCodec::codecForName("System");
    QTextCodec::setCodecForLocale(codec);
}

Txt::~Txt()
{
    delete ui;
}

void Txt::on_pushButton_2_clicked()    //选择目标txt文件
{
   // filename = QFileDialog::getOpenFileName(this);
    filename = QFileDialog::getOpenFileName(this,tr("Open txt"),".",tr("txt Files(*.txt)"));
    ui->lineEdit->setText(filename);

}

void Txt::on_pushButton_4_clicked()    //生成输出文件
{
    QFileInfo fileinfo(filename);
    int  len = filename.length();
    if(len == 0)
    {
      QMessageBox::critical(this,tr("错误"),tr("请选择txt文件！"));

      return ;
    }

    QString Path = fileinfo.absolutePath();   //路径
    QString file_name = fileinfo.fileName();  //文件名  XXXX.XXX
    QString baseName = fileinfo.baseName() ;      //文件名   xxx
    QString suffix = fileinfo.suffix();        //文件后缀
    QString wname =QString(Path+"/OUT_"+baseName+"."+suffix);

    if(suffix != "txt")
    {
      QMessageBox::critical(this,tr("错误"),tr("不是txt文件类型，请重新选择！"));

      return ;
    }

    qDebug()<< file_name ;
    qDebug()<< Path ;
    qDebug()<< wname ;

    QFile file_Read(filename);
    file_Read.open(QIODevice::ReadOnly);

    QFile file_Write(filename);
    file_Write.setFileName(wname);
    file_Write.open(QIODevice::WriteOnly);

    char* IN ;


    while(!file_Read.atEnd()) {
          QByteArray line = file_Read.readLine();
          QString str(line);
          str=str.mid(0,14);
          QByteArray ba = str.toLatin1();
          IN=ba.data();
          int cd = get_imei(IN , 14);

          if(cd == -1 )
          {
               QMessageBox::critical(this,tr("错误"),tr("txt文件内容/格式错误，请查看生成文件！"));
               file_Write.write("Err：错误发生在这里");
               ui->lineEdit_2->setText(wname);
               return;
          }

          QString Scd = QString::number(cd);

          QString  tmp = QString(str+Scd+'\n') ;
          char*  wan;
          QByteArray bar = tmp.toLatin1();
          wan=bar.data();

          file_Write.write(wan);

          qDebug()<< str+Scd;
      }

    ui->lineEdit_2->setText(wname);

    QString ts_file_name =QString("OUT_"+baseName+"."+suffix);
    QString ts = QString("Output   %1   Successfully.").arg(ts_file_name);
    char*  ch;
    QByteArray ba = ts.toLatin1();
    ch=ba.data();
    QMessageBox::information(this,tr("成功"),tr(ch));
}

void Txt::on_pushButton_3_clicked()    //打开文件
{

    QString lineEdit_2_Path = ui->lineEdit_2->text();
    int len = lineEdit_2_Path.length();
    if(len == 0)
    {
      QMessageBox::critical(this,tr("错误"),tr("无指定txt文件，打开失败"));
      return ;
    }

    bool status =QDesktopServices::openUrl(QUrl::fromLocalFile(lineEdit_2_Path));

    if(status == false)
        QMessageBox::critical(this,tr("错误"),tr("文件不存在或路径错误！"));
}

void Txt::on_pushButton_5_clicked()    //打开目录
{
    QString lineEdit_2_Path = ui->lineEdit_2->text();
    int len = lineEdit_2_Path.length();
    if(len == 0)
    {
      QMessageBox::critical(this,tr("错误"),tr("无指定目录，打开失败"));
      return ;
    }

    QFileInfo fileinfo(lineEdit_2_Path);
    QString Path = fileinfo.absolutePath();   //路径

    bool status =QDesktopServices::openUrl(QUrl::fromLocalFile(Path));

    if(status == false)
        QMessageBox::critical(this,tr("错误"),tr("文件不存在或路径错误!"));
}

void Txt::on_pushButton_clicked()      //返回主界面
{
    this->parentWidget()->show();
    delete this;
}


int Txt::get_imei(char* imei ,int len)   //计算IMEI的cd位
{
    int tmp[14];

    for(int i=0 ; i < len ;i++)
    {

        tmp[i]=imei[i] - 48;
        if(tmp[i]<0 || tmp[i] > 9 )
            return -1;
    }

    for(int i=0 ; i < len ;i++)
    {
        i++;
        tmp[i]*=2;
    }

    for(int i=0 ; i < len ;i++)
    {
        i++;
        if(tmp[i] >= 10)
        {
            tmp[i]-=9;
        }
    }

    int sum_imei = 0;
    for(int i=0 ; i < len ;i++)
    {
        sum_imei+=tmp[i];
    }

    if(sum_imei%10 == 0)
    {
        return 0;
    }
    else
    {
        imei_cd=10 - (sum_imei % 10) ;
        return imei_cd ;
    }
}



